package com.vnv.infrastructure.util;

import java.io.File;
import java.io.IOException;

import org.apache.xerces.parsers.DOMParser;
import org.springframework.context.ApplicationContext;
import org.springframework.util.Log4jConfigurer;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import com.vnv.infrastructure.exception.ErrorCode;
import com.vnv.infrastructure.exception.IPBException;
import com.vnv.infrastructure.logic.IPBAppFactory;

public class IPBInit {
	private static final String CFG_FILE = "spring/spring-config.xml";
	private boolean m_bEnableWebModules = false;
	private ApplicationContext appContext;

	public void setEnableWebModules(boolean bEnableWebModules) {
		this.m_bEnableWebModules = bEnableWebModules;
	}

	public ApplicationContext getAppContext() {
		return this.appContext;
	}

	public void setAppContext(ApplicationContext appContext) {
		this.appContext = appContext;
	}

	public void init(String cfgDir, String outputRootDir) throws IPBException {
		System.setProperty("IPEDO_CONF_PATH", cfgDir + File.separator);
		System.setProperty("IPEDO_OUTPUT_PATH", outputRootDir + File.separator);

		File logDir = new File(outputRootDir + File.separator + "logs");
		if (!(logDir.exists())) {
			logDir.mkdir();
		}

		initLogger();

		String cfgFile = cfgDir + File.separator + "spring/spring-config.xml";

		DOMParser parser = new DOMParser();
		try {
			parser.parse(cfgFile);
		} catch (Exception e) {
			e.printStackTrace();
			String[] params = { cfgFile };
			throw new IPBException(ErrorCode.CONFIG_FILE_ERROR, params, e);
		}

		Document doc = parser.getDocument();
		NodeList sections = doc.getDocumentElement().getChildNodes();

		for (int i = 0; i < sections.getLength(); ++i) {
			Node section = sections.item(i);
			if (section.getNodeType() == 1) {
				Node tempNd;
				String sectionName = section.getNodeName();

				if (sectionName.equals("appFactory")) {
					String configFile = "spring/service-config.xml, spring/manager-config.xml";

					if ((tempNd = section.getAttributes().getNamedItem(
							"configFile")) != null) {
						configFile = tempNd.getNodeValue();
					}

					if (configFile.length() <= 0)
						continue;
					IPBAppFactory.init(configFile);
				} else if (sectionName.equals("action")) {
					String accessLevel = "0";

					if ((tempNd = section.getAttributes().getNamedItem(
							"accessLevel")) != null) {
						accessLevel = tempNd.getNodeValue();
					}

					System.setProperty("IPEDO_ACTION_ACCESS_LEVEL", accessLevel);
				} else if (sectionName.equals("paging")) {
					String chunkSize = "100";

					if ((tempNd = section.getAttributes().getNamedItem(
							"chunkSize")) != null) {
						chunkSize = tempNd.getNodeValue();
					}

					System.setProperty("IPEDO_PAGING_CHUNK_SIZE", chunkSize);
				} else if (sectionName.equals("chart")) {
					if (this.m_bEnableWebModules) {
						int width = 300;
						int height = 250;
						boolean bLegend = true;
						boolean bCNFont = true;
						boolean bOverLIBToolTip = false;
						String bgColor = "238,238,238";
						String staticOutputPath = "charts";

						if ((tempNd = section.getAttributes().getNamedItem(
								"width")) != null)
							width = Integer.parseInt(tempNd.getNodeValue());
						if ((tempNd = section.getAttributes().getNamedItem(
								"height")) != null)
							height = Integer.parseInt(tempNd.getNodeValue());
						if ((tempNd = section.getAttributes().getNamedItem(
								"bgColor")) != null)
							bgColor = tempNd.getNodeValue();
						if ((tempNd = section.getAttributes().getNamedItem(
								"legends")) != null)
							bLegend = Boolean.valueOf(tempNd.getNodeValue())
									.booleanValue();
						if ((tempNd = section.getAttributes().getNamedItem(
								"cnFont")) != null)
							bCNFont = Boolean.valueOf(tempNd.getNodeValue())
									.booleanValue();
						if ((tempNd = section.getAttributes().getNamedItem(
								"overLIBToolTip")) != null)
							bOverLIBToolTip = Boolean.valueOf(
									tempNd.getNodeValue()).booleanValue();
						if ((tempNd = section.getAttributes().getNamedItem(
								"staticOutputPath")) != null) {
							staticOutputPath = tempNd.getNodeValue();
						}
						String staticOutputFullPath = outputRootDir
								+ File.separator + staticOutputPath;

						initCharter(width, height, bgColor, bLegend, bCNFont,
								bOverLIBToolTip, staticOutputPath,
								staticOutputFullPath);
					}
				} else {
					if ((!(sectionName.equals("report")))
							|| (!(this.m_bEnableWebModules)))
						continue;
					String staticOutputPath = "reports";
					String excelTemplatePath = "templates";

					if ((tempNd = section.getAttributes().getNamedItem(
							"staticOutputPath")) != null)
						staticOutputPath = tempNd.getNodeValue();
					if ((tempNd = section.getAttributes().getNamedItem(
							"excelTemplatePath")) != null) {
						excelTemplatePath = tempNd.getNodeValue();
					}
					staticOutputPath = outputRootDir + File.separator
							+ staticOutputPath;

					initReporter(staticOutputPath, excelTemplatePath);
				}
			}
		}
	}

	public void release() {
		IPBAppFactory.destroy();
		releaseLogger();
	}

	protected void initLogger() {
		try {
			String cfgDir = System.getProperty("IPEDO_CONF_PATH");
			Log4jConfigurer.initLogging(cfgDir + "properties/log4j.properties");
		} catch (IOException localIOException) {
		}
	}

	protected void releaseLogger() {
		Log4jConfigurer.shutdownLogging();
	}

	protected void initCharter(int width, int height, String bgColor,
			boolean bLegend, boolean bCNFont, boolean bOverLIBToolTip,
			String staticOutputPath, String staticOutputFullPath)
			throws IPBException {
	}

	protected void initReporter(String staticOutputPath,
			String excelTemplatePath) throws IPBException {
	}

}